package com.foreverwin.jzymes.framework.util;

import com.sap.conn.jco.*;

public class SapUtil {

	public static JCoDestination getDestination( String destName ){
		JCoDestination destination = null;
		
		try {
			destination = JCoDestinationManager.getDestination( destName );
		} catch (JCoException e) {
			e.printStackTrace();
		}
		return destination;
	}
	
	public static JCoRepository getJCoRepository( String destName ) {
		JCoRepository repository = null;
		JCoDestination destination = getDestination( destName );
		synchronized (destination) {
			try {
				repository = destination.getRepository();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return repository;
	} 
	
	public static JCoFunction getFunction( String destName, String rfcName ){
		JCoFunction jcoFunction = null;
		JCoRepository repository = null;
		repository = getJCoRepository( destName );
		try {
			jcoFunction = repository.getFunction( rfcName );
		} catch (JCoException e) {
			e.printStackTrace();
		}
		
		return jcoFunction;
	}

	public static void callFunction( String destName, String rfcName ){
		JCoDestination jcoDestination = getDestination( destName );
		JCoFunction jcoFunction = getFunction( destName, rfcName );
		
		JCoParameterList importParamlist = jcoFunction.getImportParameterList(); 
		JCoParameterList tableParamlist = jcoFunction.getTableParameterList(); 
		JCoParameterList changingParamlist = jcoFunction.getChangingParameterList();
		
		try {

			JCoContext.begin( jcoDestination );
			
			importParamlist.setValue("MATERIAL", "C11030080519906300");

			jcoFunction.execute( jcoDestination );
			
			JCoParameterList exportParamlist = jcoFunction.getExportParameterList(); 
			JCoStructure ss = exportParamlist.getStructure( "MATERIAL_GENERAL_DATA" );

			JCoContext.end( jcoDestination );
		} catch (JCoException e) {
			e.printStackTrace();
		}
	}

}
